/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dlgEditBill.java
 *
 * Created on Aug 18, 2012, 3:03:21 PM
 */

package Model.dialog;

import DataBase.DBManagerment;
import Processing.dbclass.Bill;
import Processing.dbclass.Stock;
import Processing.dbclass.StockPurchase;
import Processing.dbclass.TestPurchase;
import java.sql.Connection;
import javax.swing.JOptionPane;

/**
 *
 * @author Admin
 */
public class dlgEditBill extends javax.swing.JDialog {

    /** Creates new form dlgEditBill */
    public dlgEditBill(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * Đối tượng của class DBManagerment
     */
    private DBManagerment db = new DBManagerment();

    /**
     * Một Connection
     */
    private Connection con = db.ConnectServer();

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        pnlBillingAdd = new javax.swing.JPanel();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        txtBillAddPatientID = new javax.swing.JTextField();
        txtBillAddID = new javax.swing.JTextField();
        txtBillAddColectorID = new javax.swing.JTextField();
        jScrollPane12 = new javax.swing.JScrollPane();
        tbTestPurchase = new javax.swing.JTable();
        jLabel24 = new javax.swing.JLabel();
        jScrollPane13 = new javax.swing.JScrollPane();
        tbStockPurchase = new javax.swing.JTable();
        jLabel25 = new javax.swing.JLabel();
        rdbBillPaid = new javax.swing.JRadioButton();
        jLabel27 = new javax.swing.JLabel();
        rdbBillNotPaid = new javax.swing.JRadioButton();
        btnBillingAction = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        txtBillAmount = new javax.swing.JTextField();
        jLabel28 = new javax.swing.JLabel();
        btnAddTestPurchase = new javax.swing.JButton();
        btnEditTestPurchase = new javax.swing.JButton();
        btnDeleteTestPurchase = new javax.swing.JButton();
        btnAddStockPurchase = new javax.swing.JButton();
        btnEditStockPurchase = new javax.swing.JButton();
        btnDeleteStockPurchase = new javax.swing.JButton();
        btnAddReportInTestP = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Edit Bill");
        setResizable(false);

        pnlBillingAdd.setPreferredSize(new java.awt.Dimension(400, 480));

        jLabel17.setText("Bill's ID:");

        jLabel18.setText("Patient's ID:");

        jLabel20.setText("Collector's ID:");
        jLabel20.setToolTipText("");

        txtBillAddPatientID.setEnabled(false);

        txtBillAddID.setEnabled(false);

        txtBillAddColectorID.setEnabled(false);

        tbTestPurchase.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbTestPurchase.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        tbTestPurchase.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                tbTestPurchasePropertyChange(evt);
            }
        });
        jScrollPane12.setViewportView(tbTestPurchase);

        jLabel24.setText("Test Purchase:");

        tbStockPurchase.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbStockPurchase.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        tbStockPurchase.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                tbStockPurchasePropertyChange(evt);
            }
        });
        jScrollPane13.setViewportView(tbStockPurchase);

        jLabel25.setText("Stock Purchase:");

        buttonGroup1.add(rdbBillPaid);
        rdbBillPaid.setText("Paid");

        jLabel27.setText("Paid:");

        buttonGroup1.add(rdbBillNotPaid);
        rdbBillNotPaid.setSelected(true);
        rdbBillNotPaid.setText("Not Paid");

        btnBillingAction.setText("Update");
        btnBillingAction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBillingActionActionPerformed(evt);
            }
        });

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        txtBillAmount.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        txtBillAmount.setText("0");
        txtBillAmount.setEnabled(false);

        jLabel28.setText("Amount:");

        btnAddTestPurchase.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/smallplus.png"))); // NOI18N
        btnAddTestPurchase.setToolTipText("Add Test Purchase");
        btnAddTestPurchase.setBorder(null);
        btnAddTestPurchase.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnAddTestPurchase.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        btnAddTestPurchase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddTestPurchaseActionPerformed(evt);
            }
        });

        btnEditTestPurchase.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/smalledit.png"))); // NOI18N
        btnEditTestPurchase.setToolTipText("Edit Test Purchase");
        btnEditTestPurchase.setBorder(null);
        btnEditTestPurchase.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnEditTestPurchase.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        btnEditTestPurchase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditTestPurchaseActionPerformed(evt);
            }
        });

        btnDeleteTestPurchase.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/smalltrash.png"))); // NOI18N
        btnDeleteTestPurchase.setToolTipText("Delete Test Purchase");
        btnDeleteTestPurchase.setBorder(null);
        btnDeleteTestPurchase.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnDeleteTestPurchase.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        btnDeleteTestPurchase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteTestPurchaseActionPerformed(evt);
            }
        });

        btnAddStockPurchase.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/smallplus.png"))); // NOI18N
        btnAddStockPurchase.setToolTipText("Add Stock Purchase");
        btnAddStockPurchase.setBorder(null);
        btnAddStockPurchase.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnAddStockPurchase.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        btnAddStockPurchase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddStockPurchaseActionPerformed(evt);
            }
        });

        btnEditStockPurchase.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/smalledit.png"))); // NOI18N
        btnEditStockPurchase.setToolTipText("Edit Stock Purchase");
        btnEditStockPurchase.setBorder(null);
        btnEditStockPurchase.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnEditStockPurchase.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        btnEditStockPurchase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditStockPurchaseActionPerformed(evt);
            }
        });

        btnDeleteStockPurchase.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/smalltrash.png"))); // NOI18N
        btnDeleteStockPurchase.setToolTipText("Delete Stock Purchase");
        btnDeleteStockPurchase.setBorder(null);
        btnDeleteStockPurchase.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnDeleteStockPurchase.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        btnDeleteStockPurchase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteStockPurchaseActionPerformed(evt);
            }
        });

        btnAddReportInTestP.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/addReport.png"))); // NOI18N
        btnAddReportInTestP.setToolTipText("Add Report");
        btnAddReportInTestP.setBorder(null);
        btnAddReportInTestP.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnAddReportInTestP.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        btnAddReportInTestP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddReportInTestPActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlBillingAddLayout = new javax.swing.GroupLayout(pnlBillingAdd);
        pnlBillingAdd.setLayout(pnlBillingAddLayout);
        pnlBillingAddLayout.setHorizontalGroup(
            pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBillingAddLayout.createSequentialGroup()
                .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlBillingAddLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel18)
                            .addComponent(jLabel17)
                            .addComponent(jLabel24)
                            .addComponent(jLabel25)
                            .addComponent(jLabel20)
                            .addComponent(jLabel28)
                            .addComponent(jLabel27))
                        .addGap(18, 18, 18)
                        .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnlBillingAddLayout.createSequentialGroup()
                                .addComponent(rdbBillPaid)
                                .addGap(18, 18, 18)
                                .addComponent(rdbBillNotPaid)
                                .addGap(18, 18, 18)
                                .addComponent(btnBillingAction))
                            .addComponent(txtBillAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtBillAddID, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(pnlBillingAddLayout.createSequentialGroup()
                                .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane12, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane13, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(btnDeleteTestPurchase)
                                    .addComponent(btnEditTestPurchase)
                                    .addComponent(btnAddTestPurchase)
                                    .addComponent(btnAddReportInTestP)
                                    .addComponent(btnAddStockPurchase)
                                    .addComponent(btnEditStockPurchase)
                                    .addComponent(btnDeleteStockPurchase)))
                            .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtBillAddColectorID, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtBillAddPatientID, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE))))
                    .addGroup(pnlBillingAddLayout.createSequentialGroup()
                        .addGap(166, 166, 166)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(33, Short.MAX_VALUE))
        );
        pnlBillingAddLayout.setVerticalGroup(
            pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBillingAddLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel17)
                    .addComponent(txtBillAddID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel18)
                    .addComponent(txtBillAddPatientID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel20)
                    .addComponent(txtBillAddColectorID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlBillingAddLayout.createSequentialGroup()
                        .addComponent(btnAddTestPurchase)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnEditTestPurchase)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnDeleteTestPurchase))
                    .addGroup(pnlBillingAddLayout.createSequentialGroup()
                        .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel24)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlBillingAddLayout.createSequentialGroup()
                                .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(btnAddReportInTestP)
                                    .addComponent(jScrollPane12, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(22, 22, 22)
                                .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(pnlBillingAddLayout.createSequentialGroup()
                                        .addComponent(jScrollPane13, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(txtBillAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel28)))
                                    .addGroup(pnlBillingAddLayout.createSequentialGroup()
                                        .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel25)
                                            .addComponent(btnAddStockPurchase))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(btnEditStockPurchase)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(btnDeleteStockPurchase)))
                                .addGap(18, 18, 18)
                                .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(rdbBillPaid)
                                    .addComponent(rdbBillNotPaid)
                                    .addComponent(btnBillingAction)
                                    .addComponent(jLabel27))))
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(pnlBillingAdd, java.awt.BorderLayout.LINE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBillingActionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBillingActionActionPerformed
        // TODO add your handling code here:
        // Thông báo: Hỏi xem người dùng có muốn xóa ko, biến choice lưu lại lựa chọn của người
        // dùng ở dialog bật lên
        // Khi copy nhớ sửa lại thông báo cho phù hợp
        int choice;
        
        if (rdbBillNotPaid.isSelected()) {
            choice = JOptionPane.showConfirmDialog(this, "Are you sure to update this bill to not paid ?");
        }else{
            choice = JOptionPane.showConfirmDialog(this, "Are you sure to update this bill to paid ?");
        }

        // Nếu người dùng chọn yes ở bảng dialog bật lên
        if (choice == JOptionPane.YES_OPTION) {
            // Gọi method xóa Staff
            // Copy nhớ chỉnh lại thành method tương ứng
            bil.setAmount(Double.parseDouble(txtBillAmount.getText()));
            if (rdbBillNotPaid.isSelected()) {
                bil.setPaid(false);
            } else {
                bil.setPaid(true);
                bil.setCollectorID(staffID);
                txtBillAddColectorID.setText(staffID);
            }

            db.editBill(con, Integer.parseInt(txtBillAddID.getText()), bil);

            if (rdbBillPaid.isSelected()) {
                rdbBillNotPaid.setEnabled(false);
                rdbBillPaid.setEnabled(false);
                btnBillingAction.setEnabled(false);

                btnAddStockPurchase.setEnabled(false);
                btnAddTestPurchase.setEnabled(false);

                btnEditStockPurchase.setEnabled(false);
                btnEditTestPurchase.setEnabled(false);

                btnDeleteStockPurchase.setEnabled(false);
                btnDeleteTestPurchase.setEnabled(false);
            }
        }
        
        // DateMade và DatePaid ko phải làm vì tớ đã sửa lại method add trong
        // DBManager, sẽ lấy ngày hiện tại
        // Report có DateMade tương tự cũng được tớ sủa method rồi
}//GEN-LAST:event_btnBillingActionActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnAddTestPurchaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddTestPurchaseActionPerformed
        // TODO add your handling code here:
        dlgAddTestPurchase addTest = new dlgAddTestPurchase(null, true);

        addTest.setLocationRelativeTo(this);
        addTest.setVisible(true);

        if (addTest.getTestID() != null) {
            TestPurchase test = new TestPurchase();
            test.setBillID(Integer.parseInt(txtBillAddID.getText()));
            test.setTestID(addTest.getTestID());

            db.addTestPurchase(con, test);

            tbTestPurchase.setModel(db.searchTestPurchase(con, txtBillAddID.getText()));

            bil.setAmount(Double.parseDouble(txtBillAmount.getText()));
            bil.setPaid(false);

            db.editBill(con, Integer.parseInt(txtBillAddID.getText()), bil);
        }
    }//GEN-LAST:event_btnAddTestPurchaseActionPerformed

    private void btnDeleteTestPurchaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteTestPurchaseActionPerformed
        // TODO add your handling code here:
        if (tbTestPurchase.getSelectedRow() != -1) {
            if (JOptionPane.showConfirmDialog(this, "Are you sure to delete Test Purchase: " + tbTestPurchase.getValueAt(tbTestPurchase.getSelectedRow(), 0).toString() + " (All report of this test purchase will be deleted)?") == JOptionPane.YES_OPTION) {
                Bill bil = new Bill();

                db.deleteTestPurchase(con, tbTestPurchase.getValueAt(tbTestPurchase.getSelectedRow(), 0).toString());

                tbTestPurchase.setModel(db.searchTestPurchase(con, txtBillAddID.getText()));

                bil.setAmount(Double.parseDouble(txtBillAmount.getText()));
                bil.setPaid(false);

                db.editBill(con, Integer.parseInt(txtBillAddID.getText()), bil);
            }
        }else{
            JOptionPane.showMessageDialog(this, "Please choose a Test Purchase at the table left!");
        }
    }//GEN-LAST:event_btnDeleteTestPurchaseActionPerformed

    private void btnEditTestPurchaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditTestPurchaseActionPerformed
        // TODO add your handling code here:
        if (tbTestPurchase.getSelectedRow() != -1) {
            dlgEditTestPurchase editTest = new dlgEditTestPurchase(null, true);

            editTest.setLocationRelativeTo(this);
            editTest.setVisible(true);

            if (editTest.getTestID() != null) {
                db.editTestPurchase(con, Integer.parseInt(tbTestPurchase.getValueAt(tbTestPurchase.getSelectedRow(), 0).toString()), editTest.getTestID());
                tbTestPurchase.setModel(db.searchTestPurchase(con, txtBillAddID.getText()));

                bil.setAmount(Double.parseDouble(txtBillAmount.getText()));
                bil.setPaid(false);

                db.editBill(con, Integer.parseInt(txtBillAddID.getText()), bil);

            }
        } else {
            JOptionPane.showMessageDialog(this, "Please choose a Test Purchase at the table left!");
        }
    }//GEN-LAST:event_btnEditTestPurchaseActionPerformed

    private void btnAddReportInTestPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddReportInTestPActionPerformed
        // TODO add your handling code here:
        if (tbTestPurchase.getSelectedRow() != -1) {
            reportTestP = tbTestPurchase.getValueAt(tbTestPurchase.getSelectedRow(), 0).toString();
            this.setVisible(false);
        } else {
            JOptionPane.showMessageDialog(this, "Please choose a Test Purchase at the table left!");
        }
    }//GEN-LAST:event_btnAddReportInTestPActionPerformed

    private void btnAddStockPurchaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddStockPurchaseActionPerformed
        // TODO add your handling code here:
        dlgAddStockPurchase addStock = new dlgAddStockPurchase(null, true);

        addStock.setLocationRelativeTo(this);
        addStock.setVisible(true);

        if (addStock.getStock().getStockID() != null) {
            StockPurchase stock = new StockPurchase();
            stock.setBillID(Integer.parseInt(txtBillAddID.getText()));
            stock.setStockID(addStock.getStock().getStockID());
            stock.setNum(addStock.getStock().getNum());

            int inum = Integer.parseInt(db.searchStock(con, 1, stock.getStockID()).getValueAt(0, 3).toString());

            int dnum = Integer.parseInt(db.searchStock(con, 1, stock.getStockID()).getValueAt(0, 4).toString());

            if (stock.getNum() <= inum) {
                inum -= stock.getNum();
                dnum += stock.getNum();

                db.addStockPurchase(con, stock);

                db.editStockNum(con, stock.getStockID(), inum, dnum);

                tbStockPurchase.setModel(db.searchStockPurchase(con, txtBillAddID.getText()));

                bil.setAmount(Double.parseDouble(txtBillAmount.getText()));
                bil.setPaid(false);

                db.editBill(con, Integer.parseInt(txtBillAddID.getText()), bil);
            }else{
                JOptionPane.showMessageDialog(this, "Out of this stuff!");
            }
        }
    }//GEN-LAST:event_btnAddStockPurchaseActionPerformed

    private void tbTestPurchasePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_tbTestPurchasePropertyChange
        // TODO add your handling code here:
        double amount = 0;
        for (int i = 0; i < tbTestPurchase.getRowCount(); i++) {
            amount += Double.parseDouble(tbTestPurchase.getValueAt(i, 4).toString());
        }

        for (int i = 0; i < tbStockPurchase.getRowCount(); i++) {
            amount += Double.parseDouble(tbStockPurchase.getValueAt(i, 5).toString());
        }

        txtBillAmount.setText(""+amount);
    }//GEN-LAST:event_tbTestPurchasePropertyChange

    private void tbStockPurchasePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_tbStockPurchasePropertyChange
        // TODO add your handling code here:
        double amount = 0;
        for (int i = 0; i < tbTestPurchase.getRowCount(); i++) {
            amount += Double.parseDouble(tbTestPurchase.getValueAt(i, 4).toString());
        }

        for (int i = 0; i < tbStockPurchase.getRowCount(); i++) {
            amount += Double.parseDouble(tbStockPurchase.getValueAt(i, 5).toString());
        }

        txtBillAmount.setText(""+amount);
    }//GEN-LAST:event_tbStockPurchasePropertyChange

    private void btnEditStockPurchaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditStockPurchaseActionPerformed
        // TODO add your handling code here:
        if (tbStockPurchase.getSelectedRow() != -1) {
            StockPurchase stockP = new StockPurchase();
            
            stockP.setStockID(tbStockPurchase.getValueAt(tbStockPurchase.getSelectedRow(), 2).toString());
            
            stockP.setNum(Integer.parseInt(tbStockPurchase.getValueAt(tbStockPurchase.getSelectedRow(), 4).toString()));
            
            dlgEditStockPurchase editStock = new dlgEditStockPurchase(null, true);

            editStock.setStoc(stockP);

            editStock.setLocationRelativeTo(this);
            editStock.setVisible(true);

            if (editStock.getStock().getStockID() != null) {
                if (JOptionPane.showConfirmDialog(this, "Are you sure to edit Stock Purchase: " + tbStockPurchase.getValueAt(tbStockPurchase.getSelectedRow(), 0).toString() + " ?") == JOptionPane.YES_OPTION) {

                    db.editStockPurchase(con, Integer.parseInt(tbStockPurchase.getValueAt(tbStockPurchase.getSelectedRow(), 0).toString()), editStock.getStock());
                    tbStockPurchase.setModel(db.searchStockPurchase(con, txtBillAddID.getText()));

                    bil.setAmount(Double.parseDouble(txtBillAmount.getText()));
                    bil.setPaid(false);

                    db.editBill(con, Integer.parseInt(txtBillAddID.getText()), bil);
                }
            }
        }else{
            JOptionPane.showMessageDialog(this, "Please choose a Stock Purchase at the table left!");
        }
    }//GEN-LAST:event_btnEditStockPurchaseActionPerformed

    private void btnDeleteStockPurchaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteStockPurchaseActionPerformed
        // TODO add your handling code here:
        if (tbStockPurchase.getSelectedRow() != -1) {
            if (JOptionPane.showConfirmDialog(this, "Are you sure to delete Stock Purchase: " + tbStockPurchase.getValueAt(tbStockPurchase.getSelectedRow(), 0).toString() + " ?") == JOptionPane.YES_OPTION) {
                Bill bil = new Bill();

                db.deleteStockPurchase(con, tbStockPurchase.getValueAt(tbStockPurchase.getSelectedRow(), 0).toString());

                tbStockPurchase.setModel(db.searchStockPurchase(con, txtBillAddID.getText()));

                bil.setAmount(Double.parseDouble(txtBillAmount.getText()));
                bil.setPaid(false);

                db.editBill(con, Integer.parseInt(txtBillAddID.getText()), bil);
            }
        }else{
            JOptionPane.showMessageDialog(this, "Please choose a Test Purchase at the table left!");
        }
    }//GEN-LAST:event_btnDeleteStockPurchaseActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                dlgEditBill dialog = new dlgEditBill(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    /**
     * Biến chứa dữ liệu của bill
     */
    private Bill bil = new Bill();

    /**
     * Biến chứa id của testP muốn add report
     */
    private String reportTestP = null;

    /**
     * Bien chua gia tri cua staffID
     */
    private String staffID = null;

    public void setStaffID(String staffID) {
        this.staffID = staffID;

        if (db.searchStaff(con, 1, staffID).getValueAt(0, 3).toString().equalsIgnoreCase("Patient Register")) {
            btnAddReportInTestP.setEnabled(false);
            btnAddStockPurchase.setEnabled(false);
            btnAddTestPurchase.setEnabled(false);
            btnDeleteStockPurchase.setEnabled(false);
            btnDeleteTestPurchase.setEnabled(false);
            btnEditStockPurchase.setEnabled(false);
            btnEditTestPurchase.setEnabled(false);
        }else if(db.searchStaff(con, 1, staffID).getValueAt(0, 3).toString().equalsIgnoreCase("Tester")){
            btnBillingAction.setEnabled(false);
            btnAddReportInTestP.setEnabled(false);
        }else if(db.searchStaff(con, 1, staffID).getValueAt(0, 3).toString().equalsIgnoreCase("Doctor")){
            btnAddStockPurchase.setEnabled(false);
            btnAddTestPurchase.setEnabled(false);
            btnDeleteStockPurchase.setEnabled(false);
            btnDeleteTestPurchase.setEnabled(false);
            btnEditStockPurchase.setEnabled(false);
            btnEditTestPurchase.setEnabled(false);

            btnBillingAction.setEnabled(false);
        }
    }
    
    public String getReportTestP() {
        return reportTestP;
    }

    /**
     * Method trả về bill để chương trình xử lý
     * @return Bill
     */
    public Bill getBil() {
        return bil;
    }

    public void setBill(Bill bil) {
        this.bil = bil;

        txtBillAddID.setText(""+bil.getBillID());

        txtBillAddPatientID.setText(db.searchBilling(con, 1, ""+bil.getBillID()).getValueAt(0, 1).toString());

        tbTestPurchase.setModel(db.searchTestPurchase(con, ""+bil.getBillID()));
        tbStockPurchase.setModel(db.searchStockPurchase(con, ""+bil.getBillID()));
        txtBillAmount.setText(db.searchBilling(con, 1, ""+bil.getBillID()).getValueAt(0, 5).toString());

        if(db.searchBilling(con, 1, ""+bil.getBillID()).getValueAt(0, 8).toString().equalsIgnoreCase("Paid")){
            if (db.searchBilling(con, 1, ""+bil.getBillID()).getValueAt(0, 3) != null) {
                txtBillAddColectorID.setText(db.searchBilling(con, 1, ""+bil.getBillID()).getValueAt(0, 3).toString());
            }
            
            rdbBillPaid.setSelected(true);

            rdbBillPaid.setEnabled(false);

            rdbBillNotPaid.setEnabled(false);

            btnBillingAction.setEnabled(false);

            btnAddStockPurchase.setEnabled(false);
            btnAddTestPurchase.setEnabled(false);

            btnEditStockPurchase.setEnabled(false);
            btnEditTestPurchase.setEnabled(false);

            btnDeleteStockPurchase.setEnabled(false);
            btnDeleteTestPurchase.setEnabled(false);

        }else{
            txtBillAddColectorID.setText("");

            rdbBillNotPaid.setSelected(true);
        }

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddReportInTestP;
    private javax.swing.JButton btnAddStockPurchase;
    private javax.swing.JButton btnAddTestPurchase;
    private javax.swing.JButton btnBillingAction;
    private javax.swing.JButton btnDeleteStockPurchase;
    private javax.swing.JButton btnDeleteTestPurchase;
    private javax.swing.JButton btnEditStockPurchase;
    private javax.swing.JButton btnEditTestPurchase;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JScrollPane jScrollPane12;
    private javax.swing.JScrollPane jScrollPane13;
    private javax.swing.JPanel pnlBillingAdd;
    private javax.swing.JRadioButton rdbBillNotPaid;
    private javax.swing.JRadioButton rdbBillPaid;
    private javax.swing.JTable tbStockPurchase;
    private javax.swing.JTable tbTestPurchase;
    private javax.swing.JTextField txtBillAddColectorID;
    private javax.swing.JTextField txtBillAddID;
    private javax.swing.JTextField txtBillAddPatientID;
    private javax.swing.JTextField txtBillAmount;
    // End of variables declaration//GEN-END:variables

}
